package com.ljx.controller;

import com.ljx.common.ResponseResult;
import com.ljx.domain.dto.AddArticleDto;
import com.ljx.domain.entity.Article;
import com.ljx.loginConfig.RedisCache;
import com.ljx.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author 李捷禧
 * Date: 2023/4/18
 * ClassName: ArticleController
 */

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList(){
        ResponseResult result =  articleService.hotArticleList();
        return result;
    }

    @GetMapping("/articleList")
    public ResponseResult articleList(Integer pageNum,Integer pageSize,Long categoryId){
        return articleService.articleList(pageNum,pageSize,categoryId);
    }

    @GetMapping("/{id}")
    public ResponseResult getArticleDetail(@PathVariable("id") Long id){
        return articleService.getArticleDetail(id);
    }

    @PutMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable("id") Long id){
        return articleService.updateViewCount(id);
    }

    @PostMapping
    public ResponseResult add(@RequestBody AddArticleDto article){
        //存储到redis中
//        HashMap<String, Integer> viewCountMap = new HashMap<>();
//        viewCountMap.put(article.getId().toString(),0);
//        redisCache.setCacheMap("article:viewCount",viewCountMap);
        return articleService.add(article);
    }

    @PutMapping
    public ResponseResult updateArt(@RequestBody Article article){
        articleService.updateById(article);
        return ResponseResult.okResult("修改完成");
    }


}